import { resolve } from 'path'
import routes from './routes'
import proxy from './proxy';
const prod = process.env.NODE_ENV  === 'production'
const assetDir = "static";
export default {
  treeShaking: true,
  ignoreMomentLocale: true,
  alias:{
    '@': resolve(__dirname, 'src'),
  },
  targets: {
    ie: 11,
  },
  routes,
  proxy,
  theme: {
    'font-family-zh': 'Microsoft YaHei',
    'font-family-en': 'Arial',
  },
  plugins: [
    // ref: https://umijs.org/plugin/umi-plugin-react.html
    ['umi-plugin-react', {
      antd: true,
      dva: true,
      dynamicImport: {
        // loadingComponent: './components/common/PageLoading',
        webpackChunkName: true,
        level:1
      },
      title:{
        defaultTitle:'门户网站',
        format: '{current}{separator}{parent}',
        separator: ' - '
      },
      dll: true,

      routes: {
        exclude: [
          /models\//,
          /services\//,
          /model\.(t|j)sx?$/,
          /service\.(t|j)sx?$/,
          /components\//,
        ],
      },
      links: [{ rel: 'icon', href: '/favicon.ico' }],
      chunks: ['vendors', 'antd', 'umi'],
    }],
  ],
  chainWebpack(config){
    if(prod){
      config.merge({
        plugin: {
          install: {
            plugin: require('uglifyjs-webpack-plugin'),
            args: [{
                sourceMap: false,
                uglifyOptions: {
                    compress: {
                        // 删除所有的 `console` 语句
                        drop_console: true,
                    },
                    output: {
                        // 最紧凑的输出
                        beautify: false,
                        // 删除所有的注释
                        comments: false,
                    },
                }
            }]
          }
        },
      })
    }

    // 修改js，js chunk文件输出目录
    config.output
    .filename(assetDir + '/js/[name].[hash:8].js')
    .chunkFilename(assetDir + '/js/[name].[contenthash:8].chunk.js')

    // 修改css输出目录
    config.plugin("extract-css").tap(() => [
      {
        filename: `${assetDir}/css/[name].[contenthash:8].css`,
        chunkFilename: `${assetDir}/css/[name].[contenthash:8].chunk.css`,
        ignoreOrder: true,
      },
    ]);


    config.optimization.splitChunks({
      chunks: 'async',
      automaticNameDelimiter: '～',
      name: true,
      minSize: 30000,
      minChunks: 2,
      cacheGroups: {
        antd: {
          name: 'antd',
          test: /[\\/]node_modules[\\/](@ant-design|antd|antd-mobile)[\\/]/,
          priority: -10,
          enforce: true,
        },
        vendors: {
          name: 'vendors',
          test: /[\\/]node_modules[\\/]/,
          priority: -11,
          enforce: true,
        },
      },
    });
  }
}
